// components/calendar/calendar.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    startDate: {
      type: String,
      value: ""
    },
    endDate: {
      type: String,
      value: ""
    },
    orderArr:{
      type:Array,
      value:[]
    }

    

  },

  /**
   * 组件的初始数据
   */
  data: {
    date: "",
    dateArr: [],
    year: "",
    month: "",
    day: "",
    deliveryDays: 1,
    // startDate:"",
    // endDate:""
  },
  observers: {
    'startDate': function (e) {
      this.init()
    }
  },
  attached() {



    // this.init()

  },

  /**
   * 组件的方法列表
   */
  methods: {
    handlePauseOrder(e){
      // console.log(e)
      const {id,status} = e.currentTarget.dataset
      if(!id || !status || status!=0){
        return 
      }else{
        this.triggerEvent("handlePauseOrder",id)
      }

    },



    init() {

      const {
        startDate,
        endDate
      } = this.data;
      const startDateObj = new Date(startDate);
      const endDateObj = new Date(endDate);
      let date = startDateObj;
      let i=0;
      let arr = []
      let preMonth;
      
      while (date.valueOf() < endDateObj.valueOf()) {

        const dtime = startDateObj.getTime() + 1000 * 60 * 60 * 24 * i;
        const dateDate = new Date(dtime);
        date = dateDate
        i++;

        let dateStr = `${dateDate.getFullYear()}年${dateDate.getMonth()+1}月`;
        let dateWeek = dateDate.getDay();
        let dateMonth = dateDate.getMonth() + 1;
        let dateDay = dateDate.getDate();
        let dateYear = dateDate.getFullYear();
        
        if(dateMonth<10){
          dateMonth = "0"+dateMonth
        }
        if(dateDay<10){
          dateDay = "0"+dateDay
        }
        let orderDate = `${dateYear}-${dateMonth}-${dateDay}`
        let isNewRow = false;

        if (i === 0) {
          isNewRow = true
          preMonth = dateMonth;
          arr.push({
            isNewRow,
            dateStr
          })
        }

        if (preMonth !== dateMonth) {
          isNewRow = true
          preMonth = dateMonth;
          arr.push({
            isNewRow,
            dateStr
          })
        }
        arr.push({
          dateWeek,
          dateMonth,
          dateDay,
          dateYear,
          isMarginItem: isNewRow,
          orderDate,
       
        })
      }

      const orderDateArr = this.data.orderArr.map(item=>{
        return item.orderDate
      })


      arr = arr.map((item,index)=>{
        const i = orderDateArr.indexOf(item.orderDate)
        
        if(i ==-1){
          return item        
        }else{
          item.status = this.data.orderArr[i].status
          item.id = this.data.orderArr[i].id
          return item


        }

      })


     

      this.setData({
        dateArr: arr
      })



    }

  }
})